<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class QanswerController extends Controller
{
    public $layout = "application.views.layouts.admin";
    
  public function filters()
    {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
        	
        	array('allow',
        	'actions'=>array('List','Del','Replay','Update'),
        	'roles'=>array('admin')        	
        	),
        	array('deny',
        	'actions'=>array('List','Del','Replay','Update'),
        	'roles'=>array('guest','user')
        	)
			
		);
    }

    public function actionList()
    {
		   
		
        $answer = question::model()->findAll();
        $this->render('application.views.qanswer.list',array("answer"=>$answer));
    }

    public function actionDel()
    {
        $questionid=$_GET['id'];
        question::model()->deleteAll("question_id=:question_id",  array("question_id"=>$questionid));
        $this->redirect('/admin/qanswer/list');
    }

    public function actionReplay()
    {
        $questionid=$_GET['id'];
        $replay=question::model()->findByPk($questionid);
        $this->render('application.views.qanswer.replay',array("replay"=>$replay));
    }
    public function actionUpdate()
     {
    	if((isset($_POST['id']))&&(isset($_POST['redactor_content'])))
    	{
    		$user= new User();
    		$answer = new answer();
    		$answer->question_id= $_POST['id'];
    		$answer->message=$_POST['redactor_content'];
    		$answer->user_id = Yii::app()->user->getId();
    		$answer->save();
    		$question = question::model()->findByPk($_POST['id']);
    		$question->readed=true;
    		$question->save();
    		$this->redirect('/admin/qanswer/list');
    	}
    	else 
    	{
    		$error = 'Заполните пожалуйста все поля';
    		$this->redirect('application.views.qanswer.error',array('error'=>$error));
    	}
    }
}
?>
